﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using WMM.Tapioca;
using System.Xml;
using System.Net;
using System.Configuration;
using WMM.Tapioca.Instance;
using System;
using WMM.Tapioca.Utilities;

namespace WMM.Test.Tapioca
{
    /// <summary>
    /// The InferPrecisionTest class tests the precision inferrence code for XBRL facts. In the RECOMMENDATION 2003-12-31 + Corrected Errata
    /// 2008-07-02 version of the XBRL spec, the section on precision inferrence for XBRL facts can be found in section 4.6.6. Each test method
    /// tests the values shown in each row of the table for Example 13 on Page 53.
    /// </summary>
    [TestClass]
    public class InferPrecisionTest
    {
        static XmlCachingResolver resolver;

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            bool useProxy = Boolean.Parse(ConfigurationManager.AppSettings["UseProxy"]);
            resolver = new XmlCachingResolver(true, useProxy);    
        }
        /// <summary>
        /// Tests the example given in Row 1 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 123
        /// DECIMALS VALUE: 2
        /// INFERRED PRECISION: 5
        /// </summary>
        [TestMethod]
        public void Example13Row1()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row1.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(5, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 2 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 123.4567
        /// DECIMALS VALUE: 2
        /// INFERRED PRECISION: 5
        /// </summary>
        [TestMethod]
        public void Example13Row2()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row2.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(5, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 3 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 123e5
        /// DECIMALS VALUE: -3
        /// INFERRED PRECISION: 5
        /// </summary>
        [TestMethod]
        public void Example13Row3()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row3.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(0, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 4 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 123.45e5
        /// DECIMALS VALUE: -3
        /// INFERRED PRECISION: 5
        /// </summary>
        [TestMethod]
        public void Example13Row4()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row4.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(5, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 5 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 0.1e-2
        /// DECIMALS VALUE: 5
        /// INFERRED PRECISION: 3
        /// </summary>
        [TestMethod]
        public void Example13Row5()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row5.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(3, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 6 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 0.001E-2
        /// DECIMALS VALUE: 5
        /// INFERRED PRECISION: 1
        /// </summary>
        [TestMethod]
        public void Example13Row6()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row6.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(1, FirstFact.Precision);
        }

        /// <summary>
        /// Tests the example given in Row 7 of the table in Example 13. The row lists the following information:
        /// LEXICAL REPRESENTATION: 0.001e-3
        /// DECIMALS VALUE: 4
        /// INFERRED PRECISION: 0
        /// </summary>
        [TestMethod]
        public void Example13Row7()
        {
            InstanceDocument NewInstanceDocument = new InstanceDocument(@"..\..\..\WMM.Test.Tapioca\InferPrecisionTestDocuments\Example13Row7.xbrl", resolver);

            XbrlManager xbrlManager = new XbrlManager(NewInstanceDocument);            
            xbrlManager.Validate();

            Assert.IsTrue(NewInstanceDocument.Dts.ValidatedInstance, "The instance document was not validated.");
            Assert.IsNotNull(NewInstanceDocument.XbrlRootElement, "No XBRL fragments found.");
            Assert.AreEqual<int>(1, NewInstanceDocument.Facts.Count, "No facts found in fragment.");
            FactItem FirstFact = NewInstanceDocument.Facts[0] as FactItem;
            Assert.IsTrue(FirstFact.IsDecimalsSpecified);
            Assert.IsFalse(FirstFact.IsPrecisionSpecified);
            Assert.IsTrue(FirstFact.IsPrecisionInferred);
            Assert.AreEqual<int>(0, FirstFact.Precision);
        }
    }
}
